﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;

namespace MyMusicPlay._4._0
{
    /// <summary>
    /// 右键菜单
    /// </summary>
    public partial class MainWindow : Window
    {
        #region 歌单
        /// <summary>
        /// 删除歌单
        /// </summary>
        public void DeleteMusicList_click(object sender, RoutedEventArgs e)
        {
            var menu = sender as MenuItem;
            var name = menu.Name;
            var index = Media.GetMusicListIndex(name);
            if (index == -1)
            {
                return;
            }
            MyMessageBox.Show($"Are you sure you want to {menu.Header} the block", () =>
            {
                //清除配置
                PlayApplication.GetModule<DispositionApp>().DeleteDispositionOfMusicList(index);
                //清除缓存
                PlayApplication.GetModule<Media>().DeleteMusicList(index);
                //刷新UI
                PlayApplication.GetModule<Media>().LoadMusicList();
            });
        }
        #endregion

        #region MusicPlayingBoard
        public void PlayBoardItemMenuDel_Click(object sender, RoutedEventArgs e)
        {
            var item = sender as MenuItem;
            var musicKey = Convert.ToInt32(item.Name.Split('_')[1]);
            MusicPlayingBoard.DelMusicPlaying(musicKey);
        }

        bool IsPlayBoardItemMenuDisPlay = false;
        public void PlayBoardItemMenu_Loaded(object sender, RoutedEventArgs e)
        {
            IsPlayBoardItemMenuDisPlay = true;
        }
        public void PlayBoardItemMenu_Closed(object sender, RoutedEventArgs e)
        {
            IsPlayBoardItemMenuDisPlay = false;
        }

        #endregion

        #region MusicPlayList
        public void MusicListBoxItemMenu_Click(object sender, RoutedEventArgs e)
        {
            var item = (MusicDisplayList.SelectedValue as ListViewItem).Content;
            int key = Convert.ToInt32(((dynamic)item).Code);
            Media.InsertMusic(key);
        }

        public void MusicListBoxItemMenu_Loaded(object sender, RoutedEventArgs e)
        {
            var menu = sender as ContextMenu;
            var a = menu.Items[0] as MenuItem;
            var name = ((dynamic)(((ListViewItem)MusicDisplayList.SelectedValue).Content)).Name;
            a.Header = name;

            var width = PlayApplication.GetModule<RightMenu>().MaxWidth;
            TextBlock textBlock = new() { FontSize = 12 };
            var curWidth = SY_Library.Text.GetDisPlayWidth(name, textBlock.FontFamily, textBlock.FontStyle, textBlock.FontWeight, textBlock.FontStretch, 12);
            menu.Width = width > curWidth + 50 ? width : curWidth + 50;
        }

        public void MusicListBoxItemMenuUpateMusic_Click(object sender, RoutedEventArgs e)
        {
            var item = (MusicDisplayList.SelectedValue as ListViewItem).Content;
            int key = Convert.ToInt32(((dynamic)item).Code);
            var rander = PlayApplication.GetModule<Rander>();
            if (Media.CurDisplayList.ContainsKey(key))
            {
                Music curMusic = Media.CurDisplayList[key];
                //拉起背板
                MyMessageBox.Show(rander.CreateUpdayeMusicBox(curMusic, key));
            }
        }

        #endregion

    }
}
